//
//  FreeQuoteTableViewCell.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2023/10/23.
//

import UIKit
import AVKit

protocol FreeQuoteTableViewCellDelegate {
    func selectType(selectIndex: NSInteger)
    func selectAllImage(selectImage: [UpLoadImgModel], hh: CGFloat)
}

class FreeQuoteTableViewCell: UITableViewCell {
    var bgLayer1: CAGradientLayer!
    var bgView: UIView!
    var mileageBtn: UIButton!
    var mileageImage: UIImageView!
    var emptyMileageLab: UILabel!
    
    
    var vehicleBtn: UIButton!
    var vehicleImage: UIImageView!
    var emptyVehicleLab: UILabel!
    
    var characBtn: UIButton!
    var characImage: UIImageView!
    var emptyCharacLab: UILabel!
    
    var uploadingLab: UILabel!
    var uploadView: FreeValuationUploadView!
    var shortFilmLab: UILabel!
    var videoBtn: UIButton!
    var deleteBtn: UIButton!

    var explainLab: UILabel!
    var explainLab1: UILabel!
    var explainLab2: UILabel!
  
    var delegate: FreeQuoteTableViewCellDelegate!
    var dataModel: FreeQuoteModel!
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = .clear
        self.clipsToBounds = true
        self.selectionStyle = .none
        self.initUI()
    }
    
    private func createLayer(){
        let tempView = UIView()
    }
    
    func initUI(){
        self.bgView = UIView(frame: CGRect(x: 14, y: 0, width: kScreenWidth - 28, height: 503))
        self.bgView.layer.cornerRadius = 8
        bgLayer1 = CAGradientLayer()
        bgLayer1.locations = [0, 0.5, 0.7, 1]
        bgLayer1.frame = CGRect(x: 1, y: 1, width: kScreenWidth - 28, height: 357)
        bgLayer1.startPoint = CGPoint(x: 0.5, y: 0)
        bgLayer1.endPoint = CGPoint(x: 1, y: 1)
        let color1 = UIColor.init(hex: 0xFFF4D3).cgColor
        let color2 = UIColor.init(hex: 0xFEFFFE).cgColor
        bgLayer1.colors = [color1, color2]
        self.bgView.layer.addSublayer(bgLayer1)
        
        // shadowCode
        self.bgView.layer.shadowColor = UIColor(red: 0.96, green: 0.96, blue: 0.98, alpha: 0.8).cgColor
        self.bgView.layer.shadowOffset = CGSize(width: 0, height: 4)
        self.bgView.layer.shadowOpacity = 1
        self.bgView.layer.shadowRadius = 8
        self.contentView.addSubview(self.bgView)
        
        self.mileageBtn = UIButton(frame: CGRect(x: 14, y: 35, width: self.bgView.frame.size.width - 28, height: 46))
        self.mileageBtn.setTitle(MMLocalizedString("traffic_mileage", comment: "里程"), for: .normal)
        self.mileageBtn.titleLabel?.font = UIFont.init(name: Theme.PingFangSCRegular, size: 14)
        self.mileageBtn.backgroundColor = .white
        self.mileageBtn.layer.cornerRadius = 4
        self.mileageBtn.layer.masksToBounds = true
        self.mileageBtn.setTitleColor(Theme.titleLabColor, for: .normal)
        self.mileageBtn.semanticContentAttribute = .forceRightToLeft
        self.mileageBtn.addTarget(self, action: #selector(mileageBtnAntion), for: .touchUpInside)
        self.bgView.addSubview(self.mileageBtn)
        self.mileageBtn.tag = 100
        
        
        self.mileageImage = UIImageView(frame: CGRect(x: self.mileageBtn.frame.size.width - 24, y: 18, width: 10, height: 10))
        self.mileageImage.image = UIImage(named: "icon_drop_full")
        self.mileageBtn.addSubview(self.mileageImage)
        
        
        self.emptyMileageLab = UILabel(frame: CGRect(x: 14, y: self.mileageBtn.frame.maxY, width: self.bgView.frame.size.width - 28, height: 20))
        self.emptyMileageLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 11)
        self.emptyMileageLab.textColor = Theme.redColor
        self.bgView.addSubview(self.emptyMileageLab)
        
        
        self.vehicleBtn = UIButton(frame: CGRect(x: 14, y: self.mileageBtn.frame.maxY + 20, width: self.bgView.frame.size.width - 28, height: 46))
        self.vehicleBtn.setTitle(MMLocalizedString("vehicle_condition", comment: "车辆状况"), for: .normal)
        self.vehicleBtn.titleLabel?.font = UIFont.init(name: Theme.PingFangSCRegular, size: 14)
        self.vehicleBtn.backgroundColor = .white
        self.vehicleBtn.layer.cornerRadius = 4
        self.vehicleBtn.layer.masksToBounds = true
        self.vehicleBtn.setTitleColor(Theme.titleLabColor, for: .normal)
        self.vehicleBtn.addTarget(self, action: #selector(vehicleBtnAntion), for: .touchUpInside)
        self.bgView.addSubview(self.vehicleBtn)
        self.vehicleBtn.tag = 101
        
        
        self.vehicleImage = UIImageView(frame: CGRect(x: self.mileageBtn.frame.size.width - 24, y: 18, width: 10, height: 10))
        self.vehicleImage.image = UIImage(named: "icon_drop_full")
        self.vehicleBtn.addSubview(self.vehicleImage)
        
        
        self.emptyVehicleLab = UILabel(frame: CGRect(x: 14, y: self.vehicleBtn.frame.maxY, width: self.bgView.frame.size.width - 28, height: 20))
        self.emptyVehicleLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 11)
        self.emptyVehicleLab.textColor = Theme.redColor
        self.bgView.addSubview(self.emptyVehicleLab)
        
        
        
        self.characBtn = UIButton(frame: CGRect(x: 14, y: self.vehicleBtn.frame.maxY + 20, width: self.bgView.frame.size.width - 28, height: 46))
        self.characBtn.setTitle(MMLocalizedString("characteristic", comment: "首数"), for: .normal)
        self.characBtn.titleLabel?.font = UIFont.init(name: Theme.PingFangSCRegular, size: 14)
        self.characBtn.backgroundColor = .white
        self.characBtn.layer.cornerRadius = 4
        self.characBtn.layer.masksToBounds = true
        self.characBtn.setTitleColor(Theme.titleLabColor, for: .normal)
        self.characBtn.addTarget(self, action: #selector(characBtnAntion), for: .touchUpInside)
        self.bgView.addSubview(self.characBtn)
        self.characBtn.tag = 102
        
        
        self.characImage = UIImageView(frame: CGRect(x: self.mileageBtn.frame.size.width - 24, y: 18, width: 10, height: 10))
        self.characImage.image = UIImage(named: "icon_drop_full")
        self.characBtn.addSubview(self.characImage)
        
        
        self.emptyCharacLab = UILabel(frame: CGRect(x: 14, y: self.characBtn.frame.maxY, width: self.bgView.frame.size.width - 28, height: 20))
        self.emptyCharacLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 11)
        self.emptyCharacLab.textColor = Theme.redColor
        self.bgView.addSubview(self.emptyCharacLab)
   

        
        //图片上传view
        uploadView = FreeValuationUploadView.init(frame: CGRect.init(x: 0, y: 0, width: kScreenWidth, height: 268))
        self.uploadView.delegate = self
        self.bgView.addSubview(self.uploadView)
        
        
        
        
        self.shortFilmLab = UILabel(frame: CGRect(x: 14, y: self.uploadView.frame.maxY + 20, width: self.bgView.frame.size.width - 28, height: 20))
        self.shortFilmLab.font = UIFont.init(name: Theme.PingFangSCMedium, size: 16)
        self.shortFilmLab.textColor = Theme.titleLabColor
        self.bgView.addSubview(self.shortFilmLab)
        
        
        self.videoBtn = UIButton(frame: CGRect(x: 14, y: self.shortFilmLab.frame.maxY + 20, width: self.bgView.frame.size.width - 28, height: 75))
        self.videoBtn.backgroundColor = .white
        self.videoBtn.layer.cornerRadius = 4
        self.videoBtn.layer.masksToBounds = true
        self.videoBtn.contentMode = .scaleAspectFit
        self.videoBtn.setImage(UIImage(named: "icon_update"), for: .normal)
        self.videoBtn.addTarget(self, action: #selector(videoBtnAntion), for: .touchUpInside)
        self.bgView.addSubview(self.videoBtn)
        self.videoBtn.tag = 103
        
        
        self.deleteBtn = UIButton.init(frame: CGRect.init(x: 0, y: 0, width: 20, height: 20))
        self.deleteBtn.setImage(UIImage.init(named: "icon_close"), for: .normal)
        self.deleteBtn.setImage(UIImage.init(named: "icon_close"), for: .highlighted)
        self.deleteBtn.isHidden = false
        self.deleteBtn.addTarget(self, action: #selector(deleteBtnAction), for: .touchUpInside)
        self.videoBtn.addSubview(self.deleteBtn)
        self.deleteBtn.tag = 104
        
        
        
        self.explainLab = UILabel(frame: CGRect(x: 14, y:  20, width: self.bgView.frame.size.width - 28, height: 20))
        self.explainLab.font = UIFont.init(name: Theme.PingFangSCMedium, size: 12)
        self.explainLab.textColor = UIColor(hex: 0x939AA8)
        self.bgView.addSubview(self.explainLab)
        

        self.explainLab1 = UILabel(frame: CGRect(x: 14, y:  20, width: self.bgView.frame.size.width - 28, height: 20))
        self.explainLab1.font = UIFont.init(name: Theme.PingFangSCMedium, size: 12)
        self.explainLab1.textColor = UIColor(hex: 0x939AA8)
        self.bgView.addSubview(self.explainLab1)
        
        
        self.explainLab2 = UILabel(frame: CGRect(x: 14, y:  20, width: self.bgView.frame.size.width - 28, height: 20))
        self.explainLab2.font = UIFont.init(name: Theme.PingFangSCMedium, size: 12)
        self.explainLab2.textColor = UIColor(hex: 0x939AA8)
        self.bgView.addSubview(self.explainLab2)
                               
        
        self.emptyMileageLab.text = MMLocalizedString("please_select_vehicle_mileage", comment: "请选择车辆里程")
        self.emptyVehicleLab.text = MMLocalizedString("please_select_vehicle_status", comment: "请选择车辆状况")
        self.emptyCharacLab.text = MMLocalizedString("please_select_first_number", comment: "请选择首数")
        self.shortFilmLab.text = MMLocalizedString("upload_video", comment: "上传短片（如有）")
        self.explainLab.text = MMLocalizedString("length_least_seconds", comment: "1、片长：至少15秒")
        self.explainLab1.text = MMLocalizedString("size_up_to", comment: "大小：最多300mb")
        self.explainLab2.text = MMLocalizedString("format", comment: "格式：.3pg、mp4、mov")
        
        self.addLayout()
      
        
      
    }
    
    func addLayout(){
        
       self.bgView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.equalTo(14)
            make.width.equalTo(kScreenWidth - 28)
            make.bottom.equalTo(0).priority(.high)
        }
      
        
        self.mileageBtn.snp.makeConstraints { make in
            make.top.equalTo(35)
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.height.equalTo(46)
        }

        self.emptyMileageLab.snp.makeConstraints { make in
            make.top.equalTo(self.mileageBtn.snp.bottom)
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.height.equalTo(20)
        }


        self.vehicleBtn.snp.makeConstraints { make in
            make.top.equalTo(self.mileageBtn.snp.bottom).offset(20)
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.height.equalTo(46)
        }

        self.emptyVehicleLab.snp.makeConstraints { make in
            make.top.equalTo(self.vehicleBtn.snp.bottom)
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.height.equalTo(20)
        }


        self.characBtn.snp.makeConstraints { make in
            make.top.equalTo(self.vehicleBtn.snp.bottom).offset(20)
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.height.equalTo(46)
        }

        self.emptyCharacLab.snp.makeConstraints { make in
            make.top.equalTo(self.characBtn.snp.bottom)
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.height.equalTo(20)
        }


        
        self.uploadView.snp.makeConstraints { make in
            make.top.equalTo(characBtn.snp.bottom).offset(20)
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.height.equalTo(120)
        }


        self.shortFilmLab.snp.makeConstraints { make in
            make.top.equalTo(self.uploadView.snp.bottom).offset(20)
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.height.equalTo(22)
        }

        self.videoBtn.snp.makeConstraints { make in
            make.top.equalTo(self.shortFilmLab.snp.bottom).offset(20)
            make.left.equalTo(14)
            make.width.equalTo(100)
            make.height.equalTo(75)
            make.bottom.equalTo(self.bgView.snp.bottom).offset(-40)
        }
        
        self.deleteBtn.snp.makeConstraints { make in
            make.right.equalTo(3)
            make.top.equalTo(-3)
            make.size.equalTo(CGSize.init(width: 20, height: 20))
        }


        self.explainLab.snp.makeConstraints { make in
            make.top.equalTo(self.shortFilmLab.snp.bottom).offset(20)
            make.left.equalTo(self.videoBtn.snp.right).offset(14)
            make.right.equalTo(-14)
            make.height.equalTo(20)
        }

        self.explainLab1.snp.makeConstraints { make in
            make.top.equalTo(self.explainLab.snp.bottom).offset(2)
            make.left.equalTo(self.videoBtn.snp.right).offset(14)
            make.right.equalTo(-14)
            make.height.equalTo(20)
        }

        self.explainLab2.snp.makeConstraints { make in
            make.top.equalTo(self.explainLab1.snp.bottom).offset(2)
            make.left.equalTo(self.videoBtn.snp.right).offset(14)
            make.right.equalTo(-14)
            make.height.equalTo(20)
        }
        self.updateConstraints()
        self.layoutIfNeeded()
        self.reloadLayer()
        
    }
    
    private func reloadLayer(){//赋值高度改变进行二次渲染
        if self.bgLayer1 != nil {
            self.bgLayer1.removeFromSuperlayer()
        }
        
        bgLayer1 = CAGradientLayer()
        bgLayer1.locations = [0, 1]
        bgLayer1.frame = CGRect.init(x: 0, y: 0, width: self.bgView.bounds.size.width, height: self.bgView.bounds.size.height)
        bgLayer1.startPoint = CGPoint(x: 0, y: 0)
        bgLayer1.endPoint = CGPoint(x: 0, y: 1)
        let color1 = UIColor.init(hex: 0xFFF4D3).cgColor
        let color2 = UIColor.init(hex: 0xFEFFFE).cgColor
        bgLayer1.colors = [color1, color2]
        self.bgView.layer.insertSublayer(bgLayer1, at: 0)//把bgView放到layer上，要不复盖了
    }
    
    //setModel
    func setModel(model: FreeQuoteModel){
        self.mileageBtn.setTitle(model.mileage ?? MMLocalizedString("traffic_mileage", comment: "里程"), for: .normal)
        self.vehicleBtn.setTitle(model.condition ?? MMLocalizedString("vehicle_condition", comment: "车辆状况"), for: .normal)
        self.characBtn.setTitle(model.characteristic ?? MMLocalizedString("characteristic", comment: "首数"), for: .normal)
        self.videoBtn.setup_backgroundImage(image: model.shortImage)
        if model.shortImage == nil{
            self.videoBtn.setImage(UIImage(named: "icon_update"), for: .normal)
            self.deleteBtn.isHidden = true
        }else{
            self.videoBtn.setImage(UIImage(named: "icon_play"), for: .normal)
            self.deleteBtn.isHidden = false
        }
        
        
        if model.mileage == nil{
            self.emptyMileageLab.text = model.emptyMileag
        }else{
            self.emptyMileageLab.text = ""
        }
        
        if model.condition == nil{
            self.emptyVehicleLab.text = model.emptyVehicle
        }else{
            self.emptyVehicleLab.text = ""
        }
        
        if model.characteristic == nil{
            self.emptyCharacLab.text = model.emptyCharac
        }else{
            self.emptyCharacLab.text = ""
        }
        
        
        self.uploadView.snp.updateConstraints { make in
            make.height.equalTo(model.picuresHH)
        }
        
        self.updateConstraints()
        self.layoutIfNeeded()
        self.reloadLayer()
        
    }
        
    
    //MARK: - Antion
    //里程
    @objc func mileageBtnAntion(){
        self.delegate?.selectType(selectIndex: self.mileageBtn.tag)
    }
    
    //车辆状况
    @objc func vehicleBtnAntion(){
        self.delegate?.selectType(selectIndex: self.vehicleBtn.tag)
    }
    
    //首数
    @objc func characBtnAntion(){
        self.delegate?.selectType(selectIndex: self.characBtn.tag)
    }
    
    @objc func videoBtnAntion(){
        self.delegate?.selectType(selectIndex: self.videoBtn.tag)
    }
    
    //删除视频
    @objc func deleteBtnAction(){
        self.delegate?.selectType(selectIndex: self.deleteBtn.tag)
    }
    
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

extension FreeQuoteTableViewCell: FreeValuationUploadViewDelegate{
    
    func selectImage(array: [UpLoadImgModel], hh: CGFloat) {
        self.delegate.selectAllImage(selectImage: array, hh: hh)
    }
    
}
